package com.tfswx.jkgl.designer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.tfswx.jkgl.designer.dao.JkglXtJkdyMapper;
import com.tfswx.jkgl.designer.dto.jkgl.*;
import com.tfswx.jkgl.designer.model.JkglXtJkdy;
import com.tfswx.jkgl.designer.service.JkglService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ricky
 * @date 2021/12/5
 */
@Slf4j
@Service
public class JkglServiceImpl implements JkglService {
    @Resource
    private JkglXtJkdyMapper jkdyMapper;

    @Override
    public String addJkdy(JkdyAddInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = BeanUtil.copyProperties(inputDTO, JkglXtJkdy.class);
        jkglXtJkdy.setJkbm(IdUtil.fastSimpleUUID().toUpperCase());
        jkglXtJkdy.setXh(System.currentTimeMillis());
        jkglXtJkdy.setSfksc("Y");
        jkglXtJkdy.setSfsc("N");
        jkglXtJkdy.setCjsj(new Date());
        jkglXtJkdy.setZhxgsj(new Date());
        int i = jkdyMapper.insert(jkglXtJkdy);
        if (i != 1) {
            throw new RuntimeException("添加接口信息失败");
        }
        return jkglXtJkdy.getJkbm();
    }

    @Override
    public boolean deleteJkdy(JkdyDeleteInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = BeanUtil.copyProperties(inputDTO, JkglXtJkdy.class);
        jkglXtJkdy.setZhxgsj(new Date());
        int i = jkdyMapper.delete(jkglXtJkdy);
        if (i != 1) {
            throw new RuntimeException("删除接口信息失败");
        }
        return false;
    }

    @Override
    public boolean updateJkdy(JkdyUpdateInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = BeanUtil.copyProperties(inputDTO, JkglXtJkdy.class);
        jkglXtJkdy.setZhxgsj(new Date());
        int i = jkdyMapper.update(jkglXtJkdy);
        if (i != 1) {
            throw new RuntimeException("修改接口信息失败");
        }
        return true;
    }

    @Override
    public List<JkdyQueryResult> queryJkdy(JkdyQueryInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = BeanUtil.copyProperties(inputDTO, JkglXtJkdy.class);
        List<JkdyQueryResult> jkdyQueryResults = jkdyMapper.queryJkdy(jkglXtJkdy);

        for (JkdyQueryResult jkdyQueryResult : jkdyQueryResults) {
            jkdyQueryResult.setRcpz(JSON.parse(String.valueOf(jkdyQueryResult.getRcpz())));
            jkdyQueryResult.setCcpz(JSON.parse(String.valueOf(jkdyQueryResult.getCcpz())));
            jkdyQueryResult.setRcsl(JSON.parse(String.valueOf(jkdyQueryResult.getRcsl())));
            jkdyQueryResult.setCcsl(JSON.parse(String.valueOf(jkdyQueryResult.getCcsl())));
        }
        return jkdyQueryResults;
    }

    @Override
    public String getXmbmByJkbm(String jkbm) {
        return jkdyMapper.getXmbmByJkbm(jkbm);
    }

    @Override
    public Boolean setJkqyzt(JkqyztSetInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = BeanUtil.copyProperties(inputDTO, JkglXtJkdy.class);
        jkglXtJkdy.setZhxgsj(new Date());
        int i = jkdyMapper.setJkqyzt(jkglXtJkdy);
        if (i != 1) {
            throw new RuntimeException("设置接口启用状态失败");
        }
        return true;
    }

    @Override
    public Boolean setJkzt(JkztSetInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = BeanUtil.copyProperties(inputDTO, JkglXtJkdy.class);
        jkglXtJkdy.setZhxgsj(new Date());
        int i = jkdyMapper.setJkzt(jkglXtJkdy);
        if (i != 1) {
            throw new RuntimeException("设置接口状态失败");
        }
        return true;
    }

    @Override
    public JkdyGetOutputDTO getJkdy(JkdyGetInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = BeanUtil.copyProperties(inputDTO, JkglXtJkdy.class);
        jkglXtJkdy = jkdyMapper.get(jkglXtJkdy);
        if (jkglXtJkdy == null) {
            return null;
        }
        return BeanUtil.copyProperties(jkglXtJkdy, JkdyGetOutputDTO.class);
    }

    @Override
    public Boolean setJkflbm(JkflbmSetInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = BeanUtil.copyProperties(inputDTO, JkglXtJkdy.class);
        jkglXtJkdy.setZhxgsj(new Date());
        int i = jkdyMapper.setJkflbm(jkglXtJkdy);
        if (i != 1) {
            throw new RuntimeException("设置接口分类失败");
        }
        return true;
    }

    @Override
    public void copyJkdy(JkdyCopyInputDTO inputDTO) {
        JkglXtJkdy jkglXtJkdy = new JkglXtJkdy();
        String jkbm = IdUtil.fastSimpleUUID().toUpperCase();
        jkglXtJkdy.setJkbm(jkbm);
        jkglXtJkdy.setCjsj(new Date());
        jkglXtJkdy.setZhxgsj(new Date());
        jkdyMapper.copyJkdy(jkglXtJkdy, inputDTO.getJkbm());
    }

    @Override
    public String getXmbmByMkbsAndJkmc(String mkbs, String jkmc) {
        return jkdyMapper.getXmbmByMkbsAndJkmc(mkbs, jkmc);
    }
}
